<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Http\Controllers;


use Illuminate\Support\Facades\Input;

class UploadWeChatAvaterUrl
{

    public function handle()
    {
        //判断微信头像是否已经存储到服务器中
        $wx_id      = Input::get('wx_id');
        $avater_url = Input::get('avater_url');
        $file       = '/data/cdn_resource/pictures/wx_avater/' . $wx_id . '.jpg';
        if (!file_exists($file)) {
            if (sha1($wx_id . $avater_url) != Input::get('key')) {
                return 'key错误';
            }
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $avater_url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);//最长执行时间
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);//最长等待时间
            $img = curl_exec($ch);
            curl_close($ch);
            file_put_contents($file, $img);
        }
        return env('IMAGE_URL') . '/pictures/wx_avater/' . $wx_id . '.jpg';
    }

}